package com.hornsun.client.personalPage.api;

import com.google.common.base.Preconditions;
import com.hornsun.client.personalPage.service.AddressService;
import com.hornsun.domain.dbo.Address;
import com.hornsun.domain.dto.AddressDTO;
import com.hornsun.repository.AddressRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/4/27.
 */
@RestController
@RequestMapping("/api/address")
public class AddressAPI {

    @Resource
    private AddressRepository addressRepository;

    @Resource
    private AddressService addressService;

    /**
     * 通过openid获取地址信息
     * @param httpRequest
     * @return
     */
    @GetMapping("/getAllAddress")
    public ResponseEntity<List<Address>> getAllAddress(HttpServletRequest httpRequest){
        String openid = httpRequest.getAttribute("openid").toString();
        return new ResponseEntity<List<Address>>(addressRepository.getAddressesByOpenid(openid), HttpStatus.OK);
    }


    @GetMapping("/getAddress")
    public ResponseEntity<Address> getAddressByGuid(@RequestParam("guid")String addressGuid){
        return new ResponseEntity<Address>(addressRepository.getAddressByGuid(addressGuid),HttpStatus.OK);
    }


    /**
     * 删除地址信息
     * @param addressGuid
     * @return
     */
    @Transactional
    @PostMapping("/deleteAddress")
    public ResponseEntity<?> deleteAddress(@RequestParam("guid")String addressGuid){
        addressRepository.deleteAddressByGuid(addressGuid);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 修改地址信息
     * @param addressDTO
     * @return
     */
    @Transactional
    @PostMapping("/updateAddress")
    public ResponseEntity<?> updateAddress(HttpServletRequest httpRequest,
                                           AddressDTO addressDTO,
                                           @RequestParam("guid")String addressGuid){
        String openid = httpRequest.getAttribute("openid").toString();
        Preconditions.checkNotNull(addressGuid);
        Address address = addressService.toAddress(addressDTO);
        address.setAddressGuid(addressGuid);
        if (addressDTO.isChecked()){
            addressRepository.resetIsDefault(openid);
        }
        addressRepository.updateAddress(address);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 新增地址信息
     * @param addressDTO
     * @return
     */
    @Transactional
    @PostMapping("/addAddress")
    public ResponseEntity<?> addAddress(HttpServletRequest httpRequest,AddressDTO addressDTO){
        String openid = httpRequest.getAttribute("openid").toString();
        Address address = addressService.toAddress(addressDTO);
        address.setAddressGuid(UUID.randomUUID().toString());
        address.setOpenid(openid);
        if (addressDTO.isChecked()){
            addressRepository.resetIsDefault(openid);
        }
        addressRepository.addAddress(address);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    @PostMapping("/setDefault")
    public ResponseEntity<?> setDefault(HttpServletRequest httpRequest,@RequestParam("guid")String addressGuid){
        String openid = httpRequest.getAttribute("openid").toString();
        Address address = new Address();
        address.setAddressGuid(addressGuid);
        address.setIsDefault(true);
        addressRepository.resetIsDefault(openid);
        addressRepository.updateAddress(address);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }






}

